package com.design.YiTong.virus.service.impl;

import com.design.YiTong.model.virus.VirusInfo;
import com.design.YiTong.virus.repository.VirusRepository;
import com.design.YiTong.virus.service.virusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class virusServiceImpl implements virusService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private VirusRepository virusRepository;
    @Override
    public List searchVirus() {
        List<VirusInfo> virusInfoList = mongoTemplate.findAll(VirusInfo.class);
        return virusInfoList;
    }

    @Override
    public void saveVirus(VirusInfo virus) {
        VirusInfo virusInfo=findvirusByPoint(virus.getPoint());
        if(virusInfo==null){
            virus.setCreateTime(new Date());
            virus.setUpdateTime(new Date());
            virus.setIsDeleted(0);
            virusRepository.save(virus);
        }
        else{
            virusRepository.delete(virus);
            virus.setCreateTime(new Date());
            virus.setUpdateTime(new Date());
            virus.setIsDeleted(0);
            virusRepository.save(virus);
        }
    }

    private VirusInfo findvirusByPoint(String point) {
        VirusInfo viru=virusRepository.getAllByPoint(point);
        return viru;
    }
}
